﻿
using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Lines;
using KDS.Mes.Mes.Lines.Dtos;
using KDS.Mes.Mes.PlanModes;
using KDS.Mes.Mes.PlanModes.Dtos;
using KDS.Mes.Mes.Plans;
using KDS.Mes.Mes.Plans.Dtos;
using KDS.Mes.Mes.SeatPositions;
using KDS.Mes.Mes.SeatPositions.Dtos;
using KDS.Mes.Web.Models.Plans;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class PlansController : MesControllerBase
    {

        private readonly IPlanAppService _planAppService;
        private readonly ILineAppService _lineAppService;
        private readonly IPlanModeAppService _planModeAppService;
        private readonly ISeatPositionAppService _seatPositionAppService;

        public PlansController(
            IPlanAppService planAppService,
            ILineAppService lineAppService,
            IPlanModeAppService planModeAppService,
            ISeatPositionAppService seatPositionAppService)
        {
            _planAppService = planAppService;
            _lineAppService = lineAppService;
            _planModeAppService = planModeAppService;
            _seatPositionAppService = seatPositionAppService;


        }

        public async Task<ActionResult> Index()
        {
            //var model = new GetPlanInput {FilterText = Request.QueryString["filterText"]};
            var lines = new List<ComboboxItemDto>();
            var getLinesOutput = await _lineAppService.GetLines(new GetLineInput());
            lines = getLinesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.LineName)).ToList();

            var planModes = new List<ComboboxItemDto>();
            var getPlanModesOutput = await _planModeAppService.GetPlanModes(new GetPlanModeInput());
            planModes = getPlanModesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.PlanModeName)).ToList();

            var seatPositions = new List<ComboboxItemDto>();
            var getSeatPositionOutput = await _seatPositionAppService.GetSeatPositions(new GetSeatPositionInput());
            seatPositions = getSeatPositionOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.SeatPositionName)).ToList();


            var isOnlines = new List<ComboboxItemDto>();

            isOnlines.Insert(0, new ComboboxItemDto("", ""));
            isOnlines.Insert(1, new ComboboxItemDto("0", "未上线"));
            isOnlines.Insert(2, new ComboboxItemDto("1", "已上线"));

            var afterSaleParts = new List<ComboboxItemDto>();

            afterSaleParts.Insert(0, new ComboboxItemDto("否", "否"));
            afterSaleParts.Insert(1, new ComboboxItemDto("是", "是"));
          


            lines.Insert(0, new ComboboxItemDto("", ""));
            planModes.Insert(0, new ComboboxItemDto("", ""));
            var model = new PlansViewModel
            {
                FilterText = Request.QueryString["filterText"],
                Lines = lines,
                PlanModes = planModes, 
                IsOnlines = isOnlines,
                SeatPositions = seatPositions,
                AfterSalePart= afterSaleParts

            };

            var input = new NullableIdDto<int> { Id = null };
            var output = await _planAppService.GetPlanForEditAsync(input);
            var viewModel = new CreateOrEditPlanModalViewModel(output);

            return View(Tuple.Create(model, viewModel));
            //return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllPlans(int limit, int offset, string sortfiled, string sortway,
                                        string lineId, string planModeId, DateTime? planDate,
                                        byte? isOnline01,
                                        byte? isOnline02,
                                        byte? isOnline03,
                                        byte? isOnline04,
                                        byte? isOnline05,
                                        byte? isOnline06,
                                        byte? isOnline07,
                                        byte? isOnline08,
                                        byte? isOnline09,
                                        byte? isOnline10,
                                        int[] seatPositions
                                        //string[] seatPositions
                                        )
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetPlanInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                LineId = lineId,
                PlanModeId = planModeId,
                PlanDate = planDate,
                IsOnline01 = isOnline01,
                IsOnline02 = isOnline02,
                IsOnline03 = isOnline03,
                IsOnline04 = isOnline04,
                IsOnline05 = isOnline05,
                IsOnline06 = isOnline06,
                IsOnline07 = isOnline07,
                IsOnline08 = isOnline08,
                IsOnline09 = isOnline09,
                IsOnline10 = isOnline10,
                SeatPositions = seatPositions
            };
            var pagedTasks = _planAppService.GetPagedPlans(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Plan_Plans_Create, AppPermissions.Pages_Plan_Plans_Edit)]
        public async Task<PartialViewResult> CreateOrEditPlanModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _planAppService.GetPlanForEditAsync(input);

            var viewModel = new CreateOrEditPlanModalViewModel(output);


            return PartialView("_CreateOrEditPlanModal", viewModel);
        }


    }
}